package com.lzy.chatroom.controller;

import com.lzy.chatroom.pojo.Result;
import com.lzy.chatroom.pojo.User;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpSession;

/**
 * @author 朕爷专属pro
 */
@Controller
public class UserController {

    // 获取配置文件的测试密码
    @Value("${login.test.password}")
    private String password;

    @RequestMapping({"/","index"})
    public String toIndex(){
        return "index";
    }
    @RequestMapping("/chatroom")
    public String toChat(){
        return "chat";
    }

    /**
     * 处理用户登录请求.
     * @param user 用户登录数据
     * @param session session对象
     * @return com.lzy.pojo.Result
     */
    @PostMapping(value = "/login")
    @ResponseBody
    public Result login(@RequestBody User user, HttpSession session){
        Result result = new Result();
        if(user!= null & password != null & password.equals(user.getPassword())){
            result.setFlag(true);
            result.setMessage("登录成功！");
            // 用户名存储到Session
            session.setAttribute("username", user.getUsername());
        }else {
            result.setMessage("登录失败！");
            result.setFlag(false);
        }
        return result;
    }

     /**
     * 处理用户登录请求.
     * */
    @GetMapping("/getUsername")
    @ResponseBody
    public String getUsername(HttpSession session){
        return (String) session.getAttribute("username");
    }
}
